var editRowsNumber,_datasDatagrid, selectForm;
var formAddRow,dialogAddRow,sessionID,id,content,name,userId,createDate1,createData2;

/*Datagrid删除行*/
function deleteRow() {
	var rows = _datasDatagrid.datagrid('getSelections');
	if (rows.length > 0) {
		$.messager.confirm('请确认', '您确实要删除当前所有选择的记录吗？', function(b){
			if (b) {
				var ids = [];
				for ( var i = 0; i < rows.length; i++) {
					ids.push(rows[i].id);
				}
				$.post(sy.bp()+"/admin/user/message/delete", {ids:ids.join(","),userId:userId}, function(data) {
					if (data && data.result==1) {
						$.messager.show({msg:data.msg,title:'系统提示'});
						editRowsNumber = -1;
						_datasDatagrid.datagrid('unselectAll');
						_datasDatagrid.datagrid('reload');
					} else {
						$.messager.alert('警告', data.msg, 'warning');
					}
				}, "json");
			}
		});
	} else {
		$.messager.alert('提示', '请选择要删除的记录', 'error');
	}
};


/*Datagrid取消编辑*/
function rejectRow() {
	editRowsNumber = -1;
	_datasDatagrid.datagrid('rejectChanges');
	_datasDatagrid.datagrid('unselectAll');
};

/*查询面板查询按钮方法*/
function search_datas() {
	_datasDatagrid.datagrid('load',sy.serializeObject(selectForm));
}

/*查询面板清空按钮方法*/
function reset_datas() {
	createDate1.datetimebox('setValue','');
	createDate2.datetimebox('setValue','');
	_datasDatagrid.datagrid('load',{});
}



$(function() {
	editRowsNumber = -1;/*Datagrid操作行编号*/
	_datasDatagrid = $('#_datasDatagrid');/*数据Datagrid*/
	selectForm = $('#selectForm');/*查询面板*/
	dialogAddRow = $('#dialogAddRow');
	content = $('#content');
	userId = $('#userId').val();
	name = $('#name').validatebox({required: true});
	createDate1 = $('#createDate1').datetimebox({editable:false,showSeconds: true }).datetimebox('activeTextArrow');
	createDate2 = $('#createDate2').datetimebox({editable:false,showSeconds: true }).datetimebox('activeTextArrow');
	formAddRow = $('#formAddRow');
	id = $('#id');
	sessionID = $('#sessionID').val();
	dialogAddRow.show().dialog({resizable:false,closed:true,closable:true,modal:true});
	_datasDatagrid.datagrid({
		url : sy.bp()+'/admin/user/message/list?userId='+userId,
		fit : true,
		autoRowHeight : true,
		striped : true,
		rownumbers : true,
		pagination : true,
		singleSelect : false,
		fitColumns : true,
		pageSize : 10,
		pageList : [10,20,30,40,50],
		nowrap : true,
		border : false,
		idField : 'id',
		sortName : 'id',
		sortOrder : 'desc',
		view: sy.myview,
        emptyMsg: '没有查询到相关数据',
		toolbar: [
		          {text:'删除',iconCls: 'icon-delete',handler: deleteRow},'-',
		          {text:'撤销',iconCls: 'icon-redo',handler: rejectRow},'-',
		          {text:'刷新',iconCls: 'icon-reload',handler: function(){_datasDatagrid.datagrid('reload');}},'-',
				  ],
		frozenColumns: [[	
		                 	{field:'id',title:'编号',align:'center',width:100,checkbox:true},
						]],
			   columns:[[
		          		 {field:'name',title:'消息名',align:'left',width:100,sortable:true},
		          		 {field:'content',title:'消息内容',align:'left',width:200,sortable:true},
		          		 {field:'createName',title:'创建用户',align:'left',width:100,sortable:true},
						 {field:'createTime',title:'创建时间',align:'left',width:100,sortable:true},
				]],
		onLoadSuccess : function(data) {/*Datagrid加载完毕事件*/
			editRowsNumber = -1;
			$.messager.progress('close');
		},
		onDblClickRow : function(rowIndex, rowData) {/*Datagrid双击事件*/
			if (editRowsNumber != -1) {
				_datasDatagrid.datagrid('endEdit', editRowsNumber);
			} else {
				updateRow(rowData.id);
			}
		},
		onRowContextMenu : function(e, rowIndex, rowData){/*Datagrid右键事件*/
			e.preventDefault();	/*阻止浏览器右键菜单*/
			_datasDatagrid.datagrid('unselectAll').datagrid('selectRow', rowIndex);
			$('#datagridContextMenu').menu('show', {left:e.pageX,top:e.pageY /*取当前鼠标X和Y坐标*/});
		},
		onAfterEdit : function(rowIndex, rowData, changes) { /*结束编辑时调用的事件*/
			//console.info(rowData); 	/*rowData表示最后编辑的那些行数据*/
			//console.info(changes); 	/*changes表示更改的数据列，是一个数组*/
			var inserted = _datasDatagrid.datagrid('getChanges', 'inserted');
			//console.info(inserted);	/*inserted表示增加的那些行数据*/
			var updated = _datasDatagrid.datagrid('getChanges', 'updated');
			//console.info(updated);	/*updated表示编辑的那些行数据*/
			if (inserted.length < 1 && updated.length < 1) {
				editRowsNumber = -1;
				_datasDatagrid.datagrid('unselectAll');
				return;
			}
			var url='';
			if (inserted.length > 0) {
				url=sy.bp()+'/admin/userMessage/add';
			}
			if (updated.length > 0) {
				url=sy.bp()+'/admin/userMessage/update';
			}
			$.post(url, rowData, function(data) {
				if (data && data.result==1) {
					_datasDatagrid.datagrid('acceptChanges');
					$.messager.show({msg:data.msg,title:'系统提示'});
					_datasDatagrid.datagrid('reload');
				} else {
					_datasDatagrid.datagrid('rejectChanges');
					$.messager.alert('错误', data.msg, 'warning');
				}
				editRowsNumber = -1;
				_datasDatagrid.datagrid('unselectAll');
			}, "json");
		}
	});
	
	$(document).keydown(function(event){
		if(event.keyCode==13){
			search_datas();
		} 
	});
});

function dateFormat(s){
	////2015-07-31 16:04:51
    if (!s) return new Date();
    var y = s.substring(0,4);
    var m =s.substring(5,7);
    var d = s.substring(8,10);
    var h = s.substring(11,13);
    var min = s.substring(14,16);
    var sec = s.substring(17,19);
	//console.info(y+"=="+m+"=="+d+"=="+h+"=="+min+"=="+sec)
    if (!isNaN(y) && !isNaN(m) && !isNaN(d) && !isNaN(h) && !isNaN(min) && !isNaN(sec)){
      return new Date(y,m-1,d,h,min,sec);
    } else {
      return new Date();
    }
  }

function dateCompare(time){
	time = dateFormat(time);
	var current = new Date();
	var message = new Date(time);
	if(current > message){
		return true;
	}
	return false;
}
